package com.jinly.ryder.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.manage.dao.DatabaseInstanceMapper;
import com.jinly.ryder.manage.dao.entity.DatabaseInstanceDO;
import com.jinly.ryder.manage.service.DatabaseInstanceService;
import com.jinly.ryder.manage.service.InstanceSecretService;
import com.jinly.ryder.manage.service.mapping.DatabaseInstanceMapping;
import com.jinly.ryder.manage.service.model.DatabaseInstance;
import com.jinly.ryder.manage.service.model.InstanceSecret;
import com.jinly.ryder.manage.service.model.InstanceType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class DatabaseInstanceServiceImpl implements DatabaseInstanceService {

    private final DatabaseInstanceMapper databaseInstanceMapper;
    private final DatabaseInstanceMapping databaseInstanceMapping;
    private final InstanceSecretService instanceSecretService;

    @Override
    public List<DatabaseInstance> list(String appName) {
        Wrapper<DatabaseInstanceDO> queryWrapper = new QueryWrapper<DatabaseInstanceDO>().lambda()
                .eq(DatabaseInstanceDO::getAppName, appName);
        return databaseInstanceMapping.toModelList(databaseInstanceMapper.selectList(queryWrapper));
    }

    @Override
    public Page<DatabaseInstance> instancePage(Page page, DatabaseInstance query) {
        Wrapper<DatabaseInstanceDO> queryWrapper = new QueryWrapper<DatabaseInstanceDO>().lambda()
                .eq(query.getAppName() != null, DatabaseInstanceDO::getAppName, query.getAppName())
                .eq(query.getName() != null, DatabaseInstanceDO::getName, query.getName())
                .eq(query.getEnvType() != null, DatabaseInstanceDO::getEnvType, query.getEnvType())
                .like(query.getHost() != null, DatabaseInstanceDO::getHost, query.getHost());
        return databaseInstanceMapping.toModelPage(databaseInstanceMapper.selectPage(page, queryWrapper));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(DatabaseInstance databaseInstance) {
        if (databaseInstance.getId() == null) {
            log.info("新增={}", databaseInstance);
            DatabaseInstanceDO databaseInstanceDO = databaseInstanceMapping.toEntity(databaseInstance);
            databaseInstanceMapper.insert(databaseInstanceDO);
            databaseInstance.setId(databaseInstanceDO.getId());
        } else {
            log.info("更新={}", databaseInstance);
            databaseInstanceMapper.updateById(databaseInstanceMapping.toEntity(databaseInstance));
        }
        if (!ObjectUtils.isEmpty(databaseInstance.getUsername())
                && !ObjectUtils.isEmpty(databaseInstance.getPassword())) {
            InstanceSecret secret = new InstanceSecret();
            secret.setInstanceType(InstanceType.DATABASE);
            secret.setInstanceId(databaseInstance.getId());
            secret.setSecretName(databaseInstance.getUsername());
            secret.setPlainText(databaseInstance.getPassword());
            instanceSecretService.saveSecret(secret);
        }
    }

    @Override
    public DatabaseInstance get(Integer instanceId) {
        return databaseInstanceMapping.toModel(databaseInstanceMapper.selectById(instanceId));
    }
}
